import Flutter
import UIKit
//import Masonry
import CoreLocation

let screenWidth = UIScreen.main.bounds.width
let screenHeight = UIScreen.main.bounds.height


//public class LocationManager {
//
//    var location:MapLocationProtocol!
//
//    static let share = LocationManager()
//
//    init() {
//        guard let factory = MapEngine.getFactory(appkey: "sxY7s9ld6q1okALErGmbdI0sIpDlO0MF") else { return }
//        self.location = factory.getMapLocation()
//    }
//
//}

public class SwiftBaiduMapviewPlugin: NSObject, FlutterPlugin {

//  var location:MapLocationProtocol!

  public static func register(with registrar: FlutterPluginRegistrar) {
    let channel = FlutterMethodChannel(name: "baidu_map_plugin", binaryMessenger: registrar.messenger())
    let instance = SwiftBaiduMapviewPlugin()
    registrar.register(BadiduMapViewFactory(messenger:registrar.messenger()), withId: "plugins.baidumapplugin_example/mapview")
    registrar.addMethodCallDelegate(instance, channel: channel)

//    let location = LocationManager.share.location
  }

  public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
    switch call.method {
    case "getPlatformVersion":
        result("iOS " + UIDevice.current.systemVersion)
    case "getLocation":
        if !CLLocationManager.locationServicesEnabled() { // 用户为打开定位权限
            result(["success":false,"errorInfo":"未打开定位服务"])
            return
        }
        let status = CLLocationManager.authorizationStatus()
        switch status {
        case .restricted:
            result(["success":false,"errorInfo":"定位服务受限"])
        case .notDetermined://未做出选择
            fallthrough
        case .authorizedWhenInUse:
            fallthrough
        case .authorizedAlways:
        fallthrough
//            _ = LocationManager.share.location.requestLocation(with: true, netWorkState: true) { (location, error) in
//                if let coordinate = location?.getLocation()?.coordinate {
//                    result(["success":true,"longitude":coordinate.longitude,"latitude":coordinate.latitude])
//                } else {
//                    result(["success":false,"errorInfo":error.debugDescription])
//                }
//            }
        case .denied:
            result(["success":false,"errorInfo":"已拒绝APP使用定位服务"])
        }
    default:
        result("default")
    }
  }
}


